package changgou.pay.mq;


import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

public class MQConfig {

    /**
     * 读取配置文件中的信息对象。
     */
    @Autowired
    private Environment env;

    /**
     * 创建队列
     */
        @Bean
        public Queue orderQueue(){
        return new Queue(env.getProperty("mq.pay.queue.order"));
    }


    /**
     * 创建交换机
     */
    @Bean
    public Exchange basicExchange(){
        return new DirectExchange(env.getProperty("mq.pay.exchange.order"),true,false);
    }

    /**
     * 队列绑定交换机
     */
    @Bean
    public Binding basicBinding(Queue orderQueue, Exchange orderExchange){
        return BindingBuilder.bind(orderQueue).to(orderExchange).with(env.getProperty("mq.pay.routing.key")).noargs();
    }

    /***
     * 创建seckill交换机
     * @return
     */
    @Bean
    public Exchange seckillExchange(){
        return new DirectExchange(env.getProperty("mq.pay.exchange.seckillorder"),true,false);
    }

    /***
     * 创建seckill队列
     * @return
     */
    @Bean
    public Queue seckillQueue(){
        return new Queue(env.getProperty("mq.pay.queueseckillorder"),true);
    }

    /****
     * 队列绑定到交换机上
     * @return
     */
    @Bean
    public Binding basicBindingSeckillOrder(Queue seckillQueue,Exchange seckillExchange){
        return BindingBuilder.bind(seckillQueue).to(seckillExchange).with(env.getProperty("mq.pay.routing.seckillkey")).noargs();
    }


}
